<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
        #app{
            height: 100%;
        }
        /deep/ .el-container{
            height: 100%!important;
        }
    </style>

</head>
<body>
<div id="app">


    <!--搜索表单-->

    <el-form :inline="true" class="demo-form-inline">


        <el-form-item label="项目名称">
            <el-input v-model="name" placeholder="请输入项目名称"></el-input>
        </el-form-item>


        <el-form-item>
            <el-button type="primary" @click="query">查询</el-button>
        </el-form-item>

        <el-button type="primary"  @click="dialogVisible = true">新增</el-button>
    </el-form>



    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑项目"
            :visible.sync="dialogVisible"
            width="30%"
    >

        <el-form ref="form" :model="project" label-width="80px">
            <el-form-item label="项目名称">
                <el-input v-model="project.projectName"></el-input>
            </el-form-item>
            <el-form-item label="项目编码">
                <el-input v-model="project.projectCode"></el-input>
            </el-form-item>
            <el-form-item label="项目周期">
                <el-input v-model="project.projectCycle"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addProject">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
        <!--    <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>

            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>-->

            <el-table-column
                    prop="projectName"
                    label="项目名称"
                    align="center"
            >
            </el-table-column>

            <el-table-column
                    prop="projectCode"
                    label="项目编码"
                    align="center"
            >
            </el-table-column>

            <el-table-column
                    prop="projectCycle"
                    label="项目周期"
                    align="center"
            >
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">

                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" size="mini" @click="toEdit(scope.row)">修改</el-button>
                        <el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>
                    </el-row>
                </template>


            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="tatal">
    </el-pagination>

</div>

<script src="/js/axios-0.18.0.js"></script>
<script src="/js/axios-interceptor.js"></script>
<script src="../../js/vue.js"></script>
<script src="../../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../../element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            toEdit(row){
                this.project = row;
                this.dialogVisible = true;
            },
            del(id){
                this.$confirm('确认删除？')
                    .then(_ => {
                        let _this = this;
                        axios.delete("/proj/"+id).then((res)=>{
                            if(res.data.flag){
                                _this.$message.success("删除成功！");
                                this.query();
                            }else {
                                _this.$message.error(res.data.data);
                            }
                        });
                    })
                    .catch(_ => {

                    });
            },
            // 复选框选中后执行的方法
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log(this.multipleSelection)
            },
            // 查询方法
            query() {
                console.log(this.project);
                axios.get("/proj?page="+this.currentPage+"&pageSize="
                    +this.pageSize+"&name="+this.name)
                    .then((res)=>{
                    this.tableData=res.data.data.records;
                    this.total = res.data.data.total;
                    this.project={};
                });
            },
            // 添加或修改数据
            addProject(){
                console.log(this.project);
                if(this.project.id){
                    axios.put("/proj",this.project).then((res)=>{
                        if(res.data.flag){
                            this.dialogVisible=false;
                            this.project={};
                            this.query();
                        }else{
                            this.$message.error(res.data.data);
                        }
                    });
                }else {
                    axios.post("/proj",this.project).then((res)=>{
                        if(res.data.flag){
                            this.dialogVisible=false;
                            this.project={};
                            this.query();
                        }else{
                            this.$message.error(res.data.data);
                        }
                    });
                }

            },

            //分页
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pagesize = val;
                this.query();
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.query();
            }

        },
        data() {
            return {
                name:"",
                // 当前页码
                currentPage: 1,
                pageSize:5,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,

                // 品牌模型数据
                project: {
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 表格数据
                tableData: [],
                total:0
            }
        },
        mounted(){
            this.query();
        }
    })

</script>

</body>
</html>
